<?php

/**
 * Fait appel aux templates concernés
 *
 * @package cass
 * @author Mickaël CASTANHEIRO <mickael.castanheiro@gmail.com>
 * @version 1.0
 */

namespace cass;

class ViewController extends Singleton {

    /**
     * Constructeur du ViewController
     */
    protected function __construct() {

    }

    /**
     * Retourne le contenu d'un template
     * @param <type> $_package
     * @param <type> $_action
     * @param <type> $_request
     * @param <type> $_parameters
     * @return <type> 
     */
    public function getRender($_uri, $_parameters) {
        $package = formatSeparatorToUpper($_uri->package, '-', false);
        $action = $_uri->action;
        $request = formatSeparatorToUpper($_uri->request, '-', true);

        $fileName = __PATH__ . Data::directories('templates') . $package . '/' . $action . $request . Data::extensions('templates');
        if (!file_exists($fileName)) {
            $fileName = __PATH__ . Data::directories('templates') . '_default' . Data::extensions('templates');
        }

        $vars = array(
            '__title' => $_parameters->getTitle(),
            '__css'   => $_parameters->getCss(),
            '__ariane'=> $_parameters->getAriane(),
            '__js'    => $_parameters->getJs(),
        );
        $vars = array_merge($vars, $_parameters->getVars());
        $template = new Template;
        $template->setFileName($fileName);
        $template->setVars($vars);
        return $template->getRender();
    }

   /**
    * Affiche du texte
    * @param String $_render
    */
    public function render($_render) {
        ob_start();
        echo $_render;
        ob_end_flush();
    }

   /**
    * Affiche du texte dans la structure
    * @param String $_render
    */
    public function renderWithStructure($_render, $_structure=null, Parameters $_parameters = null) {
        $template = new Template; 
        $template->setName($_structure);
        
        $vars = array(
            '__title' => $_parameters->getTitle(),
            '__css'   => $_parameters->getCss(),
            '__ariane'=> $_parameters->getAriane(),
            '__js'    => $_parameters->getJs(),
        );
        $vars = array_merge($vars, $_parameters->getVars());
        $template->setVars($vars);
        $template->addToVars('__content', $_render);
        $render = $template->getRender();

        $template->render(/*'ob_gzhandler'*/);
    }

  

}